package com.hxqc.basic.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hxqc.basic.common.StringUtil;
import com.hxqc.basic.common.TaoBaoUtils;
import com.hxqc.basic.serivce.CommonService;

@Controller
@RequestMapping("/log")
public class SysLogController {

	private static Logger logger = LoggerFactory.getLogger(SysLogController.class);
	@Resource
	private CommonService commonService;
	@Autowired
	HttpServletRequest request;
	
	/**
	 * 登录日志列表
	 * @param param
	 * @return  
	 * @throws InterruptedException 
	 */
	@RequestMapping(value = "/loginList")
	@ResponseBody
	public Map<String, Object> loginList(@RequestParam Map<String, String> param) {
		Map<String, Object> result = new HashMap<String, Object>();
		int count = commonService.countSysLoginLog(param);
		List<Map<String, Object>> data = commonService.findSysLoginLog(param);
		result.put("iTotalRecords", count);
		result.put("iTotalDisplayRecords", count);
		result.put("aaData", data);
		return result;
	}
		

	/**
	 * 操作日志列表
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/optionList")
	@ResponseBody
	public Map<String, Object> optionList(@RequestParam Map<String, String> param) {
		Map<String, Object> result = new HashMap<String, Object>();
		int count = commonService.countSysOptionLog(param);
		List<Map<String, Object>> data = commonService.findSysOptionLog(param);
		result.put("iTotalRecords", count);
		result.put("iTotalDisplayRecords", count);
		result.put("aaData", data);
		return result;
	}
	
	@RequestMapping(value = "/taobaoSearchList")
	@ResponseBody
	public Map<String, Object> taobaoSearchList(@RequestParam Map<String, String> param) {
		ServletContext context = request.getSession().getServletContext();
		String keyWord = param.get("search_value");//要搜索宝贝名称、关键字
		String expectPrice = param.get("search_key");//浮动范围
		logger.info("用户输入的参数 ： " + "keyWord : " + keyWord + " , expectPrice : " + expectPrice);
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
		Map<String,Object> per = new HashMap<String,Object>();
		
		if(StringUtil.isNotBlank(keyWord) && StringUtil.isNotBlank(expectPrice)){
			logger.info("非空");
			Integer searchPrice = TaoBaoUtils.taobaoAdvanceSearch(keyWord);//淘宝网价格
			Integer myPrice = Integer.parseInt(expectPrice);//输入价格
			
			Integer lastPrice = 0;
			if(context.getAttribute("price") != null){
				lastPrice = Integer.parseInt((String)context.getAttribute("price"));
			};
			logger.info("上一次价格 : " + lastPrice);
//			if(searchPrice > myPrice){
//				per.put("name", keyWord);
//				per.put("price", searchPrice.toString() + " 价格溢出");
//				per.put("priceMine", myPrice);
//				data.add(per);
//			}else{
//				per.put("name", keyWord);
//				per.put("price", searchPrice.toString());
//				per.put("priceMine", myPrice);
//				data.add(per);
//			}
			if(lastPrice != 0){
				if(Math.abs(lastPrice - searchPrice) >= myPrice){
					//说明波动范围超过用户设置的值
					per.put("name", keyWord);
					per.put("price", searchPrice.toString() + " 价格达到预警值");
					per.put("priceMine", myPrice);
					data.add(per);
				}else{
					per.put("name", keyWord);
					per.put("price", searchPrice.toString());
					per.put("priceMine", myPrice);
					data.add(per);
				}
			}else{
				per.put("name", keyWord);
				per.put("price", searchPrice.toString());
				per.put("priceMine", myPrice);
				data.add(per);
			}
			result.put("iTotalRecords", 1);
			result.put("iTotalDisplayRecords", 1);
			result.put("aaData", data);
			
			context.setAttribute("price", searchPrice.toString());//将此次价格保存以便进行比较 
		}else{
			logger.info("空");
			per.put("name", "");
			per.put("price", "");
			per.put("priceMine", "");
			data.add(per);
			result.put("iTotalRecords", 0);
			result.put("iTotalDisplayRecords", 0);
			result.put("aaData",data);
		}
		
		logger.info("result : " + result.toString());
		return result;
	}
	
}
